package com.gin.kotlin.gf2gamecommunity.navi

import android.util.Log
import androidx.navigation.NavHostController
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.Navigator
import com.gin.kotlin.gf2gamecommunity.routes.AccountManageRoute
import com.gin.kotlin.gf2gamecommunity.routes.CommentListRoute
import com.gin.kotlin.gf2gamecommunity.routes.CommentSingleRoute
import com.gin.kotlin.gf2gamecommunity.routes.ExchangeItemRoute
import com.gin.kotlin.gf2gamecommunity.routes.GameInfoRoute
import com.gin.kotlin.gf2gamecommunity.routes.ImagePreViewRoute
import com.gin.kotlin.gf2gamecommunity.routes.MessageListRoute
import com.gin.kotlin.gf2gamecommunity.routes.PublishRoute
import com.gin.kotlin.gf2gamecommunity.routes.ReplyRoute
import com.gin.kotlin.gf2gamecommunity.routes.ScoreLogRoute
import com.gin.kotlin.gf2gamecommunity.routes.SearchRoute
import com.gin.kotlin.gf2gamecommunity.routes.StrategyListRoute
import com.gin.kotlin.gf2gamecommunity.routes.ThemeDetailRoute
import com.gin.kotlin.gf2gamecommunity.routes.ThemeListRoute
import com.gin.kotlin.gf2gamecommunity.routes.TopicListRoute
import com.gin.kotlin.gf2gamecommunity.routes.UserCenterRoute
import com.gin.kotlin.gf2gamecommunity.routes.UserRelatedListRoute
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull

class GF2Navigator(navHostController: NavHostController) : Navigator(navHostController) {
    fun onNavigateToTopicList(route: TopicListRoute) = nav(route)
    fun onNavigateToAccountManage(route: AccountManageRoute) = nav(route)
    fun onNavigateToExchangeItem(route: ExchangeItemRoute) = nav(route)
    fun onNavigateToUserCenter(route: UserCenterRoute) = nav(route)
    fun onNavigateToStrategyList(route: StrategyListRoute) = nav(route)
    fun onNavigateToScoreLog(route: ScoreLogRoute) = nav(route)
    fun onNavigateToMessageList(route: MessageListRoute) = nav(route)
    fun onNavigateToThemeList(route: ThemeListRoute) = nav(route)
    fun onNavigateToCommentList(route: CommentListRoute) = nav(route)
    fun onNavigateToGameInfo(route: GameInfoRoute) = nav(route)
    fun onNavigateToUserRelatedList(route: UserRelatedListRoute) = nav(route)
    fun onNavigateToCommentSingle(route: CommentSingleRoute) = nav(route)
    fun onNavigateToImagePreView(route: ImagePreViewRoute) = nav(route)
    fun onNavigateToThemeDetail(route: ThemeDetailRoute) = nav(route)
    fun onNavigateToSearch(route: SearchRoute) = nav(route)
    fun onNavigateToReply(route: ReplyRoute) = nav(route)
    fun onNavigateToPublish(route: PublishRoute) = nav(route)
    fun onNavigateByUrl(url: String) {
        val httpUrl = url.toHttpUrlOrNull()
        if (httpUrl == null) {
            App.toast("非法的链接: $url")
            Log.w(TAG, "非法的链接: $url")
            return
        }

        val urlRoute = UrlRoute.firstOrNull(httpUrl)
        if (urlRoute == null) {
            App.toast("未识别的链接: $url")
            Log.w(TAG, "未识别的链接: $url")
        } else {
            nav(urlRoute.toRoute(httpUrl))
        }
    }

    fun openable(url: String?) = url?.toHttpUrlOrNull()?.let { UrlRoute.firstOrNull(it) } != null

    companion object {
        private val TAG = GF2Navigator::class.java.simpleName
    }
}